<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class CzgzController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("CzGz");

	}
	
	function index(){
		
		




		$list=$this->db->order ( "price asc")->select();
		

		$this->assign("list",$list);
		
		$this->display();
	}
	

	
	
	function add(){

		if(IS_POST){

			if ($this->db->create($_POST)) {
				if ($this->db->add()!==false) {


					$this->success("添加成功！", U("index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{


			$this->display("form");
		}
		}

	
	function edit(){
		
		
		if(IS_POST){
			if ($this->db->create($_POST)) {
				
				if ($this->db->save()!==false) {

					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		
		}else{
	
		$id= intval(I("get.id"));
		if(!$id){
			$this->error("缺少参数！");
		}

		$r=$this->db->where("id=$id")->find();
		$this->assign("edit",true);
		$this->assign("data",$r);
		$this->display("form");
		}
	}


	/**
	 * 改变默认值
	 */
	public function changedefault() {
		$id = $_REQUEST[id];



		$r = $this->db->where(array("id" => $id))->find();

		$status = $r['default'] ? 0 : 1;





			$result1=$this->db->query("update `__PREFIX__cz_gz` SET `default`=0 ");
			$result=$this->db->save(array("default"=>$status,"id"=>$id));

			if ($result1 !== false&&$result !== false) {

				$this->success("转换成功！");

			} else {
				$this->error("转换失败！");
			}
	}




	}